﻿/*----------------------------------------------------------------
 * 版权：http://www.zhugekeji.com
 * 
 * 文件名： IniConfig
 * 文件功能描述： 读写注册表中的配置
 * 
 * 作者：ZhuGe
 * 日期：2012年9月24日
 * 
 * 当前版本：V1.0.0
 * 
 * 修改日期：
 * 修改内容：
 *---------------------------------------------------------------*/


/*----------------------------------------------------------------
 * 
 * 默认注册表键为：HKEY_LOCAL_MACHINE\Software
 * 示意图如下：
 * 
 * HKEY_LOCAL_MACHINE          |   
 * －Software                          |   Key1   Value1
 * －－程序项（SoftwareName）   |    
 * －－－子项（Section）            |   KeyN   ValueN
 * 
 *---------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Win32;
using System.Diagnostics;

namespace ZhuGe.Common.Config
{
    class RegConfig
    {
        private string softwareName;
        private bool isConfig;
        RegistryKey registryKey;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="softwareName">程序名称：对应注册表中Software项下的程序项</param>
        public RegConfig(string softwareName)
        {
            this.SoftwareName = softwareName;
        }

        /// <summary>
        /// 程序名称：对应注册表中Software项下的程序项
        /// </summary>
        public string SoftwareName
        {
            set
            {
                softwareName = value;
                isConfig = OnSoftwareNameChange();
            }
        }

        /// <summary>
        /// 读取配置
        /// </summary>
        /// <param name="section">程序项下的子项</param>
        /// <param name="key">键</param>
        /// <param name="value">返回的键值</param>
        /// <returns>是否读取成功</returns>
        public bool ReadConfig(string section, string key, ref string value)
        {
            bool isRead = false;
            try
            {
                if (isConfig)
                {
                    RegistryKey skey = this.registryKey.OpenSubKey(section);
                    if (skey != null)
                    {
                        value = skey.GetValue(key).ToString();
                        isRead = true;
                    }
                }

            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            return isRead;
        }

        /// <summary>
        /// 写入配置
        /// </summary>
        /// <param name="section">程序项下的子项</param>
        /// <param name="key">键</param>
        /// <param name="value">写入的键值</param>
        /// <returns>是否写入成功</returns>
        public bool WriteConfig(string section, string key, string value)
        {
            bool isWrite = false;
            try
            {
                if (isConfig)
                {
                    RegistryKey skey = this.registryKey.OpenSubKey(section, true);
                    if (skey != null)
                    {
                        skey.SetValue(key, value);
                        isWrite = true;
                    }
                    else    //如果不存在该键则创建
                    {
                        skey = this.registryKey.CreateSubKey(section);
                        skey.SetValue(key, value);
                        isWrite = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            return isWrite;
        }

        #region 私有方法
        private bool OnSoftwareNameChange()
        {
            bool isLoad = false;
            try
            {
                //打开该项
                registryKey = Registry.LocalMachine.OpenSubKey("Software\\" + softwareName, true);
                if (registryKey == null)
                {
                    //如果不存在则创建该项
                    registryKey = Registry.LocalMachine.CreateSubKey("Software\\" + softwareName);
                }
                isLoad = true;
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            return isLoad;
        }
        #endregion
    }
}
